package health.lm.com.mine.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.adapter.RecordListAdapter;
import health.lm.com.mine.entity.RecordListBean;
import health.lm.com.mine.present.RecordListPresenter;
import health.lm.com.router.ARouterConstant;

/**
 * 提现记录
 * @author Administrator
 */
@Route(path = ARouterConstant.RecordListActivity)
public class RecordListActivity extends XListActivity<RecordListPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.rlv_deal_list)
    RecyclerView rlvDealList;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout srlLayout;
    private boolean isRefresh;
    private List<RecordListBean.Lists> beanList;
    private RecordListAdapter adapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_records_list;
    }

    @Override
    public RecordListPresenter newP() {
        return new RecordListPresenter();
    }
    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        super.rlRefreshLayout = srlLayout;
        super.adapter = adapter;
        super.recyclerView = rlvDealList;
        super.initData(savedInstanceState);
        titlebar.setListener((v, action, extra) -> {
            finish();
        });
    }

    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        this.isRefresh = isRefresh;
        getP().getDealList(isRefresh,layout, page, pageSize);
    }



    /**
     * 初始化适配器
     */
    private void initAdapter() {
        beanList = new ArrayList<>();
        adapter = new RecordListAdapter(beanList);
        rlvDealList.setLayoutManager(new LinearLayoutManager(mActivity));
        rlvDealList.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)
                .setDividerColor(0xFFeeeeee)
                .setOrientation(RecyclerView.VERTICAL)
                .setIsShowLastDivide(true)));
        rlvDealList.setAdapter(adapter);
    }


    public void getDealListSuccess(List<RecordListBean.Lists> beanlist) {
        this.beanList = beanlist;
        if (beanList == null ) {
            return;
        }
        if(isRefresh&&beanList.size()>= pageSize){
            initLoadMore();
        }
        notifyData();
    }

    private void notifyData() {
        if(isRefresh) {
            adapter.setNewData(beanList);
        }else {
            adapter.addData(beanList);
        }
        if(beanList.size()<pageSize){
            adapter.loadMoreEnd();
        }else {
            adapter.loadMoreComplete();
        }
        if (beanList.size()<=0){
            adapter.setEmptyView(empty());
        }
    }


}
